package com.repair.controller;

import com.repair.entity.RepairmanOrder;
import com.repair.service.RepairmanOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/repairman/order-records")
public class RepairmanOrderController {

    @Autowired
    private RepairmanOrderService repairmanOrderService;

    @GetMapping("/list")
    public ResponseEntity<List<RepairmanOrder>> getRepairmanOrders(@RequestParam Long repairmanId) {
        return ResponseEntity.ok(repairmanOrderService.getRepairmanOrders(repairmanId));
    }

    @GetMapping("/listByUser")
    public ResponseEntity<List<RepairmanOrder>> listByUser(@RequestParam Long repairmanId) {
        return ResponseEntity.ok(repairmanOrderService.getRepairmanOrders(repairmanId));
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<RepairmanOrder> getOrderDetail(@PathVariable Long orderId) {
        return ResponseEntity.ok(repairmanOrderService.getOrderDetail(orderId));
    }

    @PostMapping("/accept")
    public ResponseEntity<?> acceptOrder(@RequestBody Map<String, Object> request) {
        Long repairmanId = Long.parseLong(request.get("repairmanId").toString());
        Long orderId = Long.parseLong(request.get("orderId").toString());
        String remark = (String) request.get("remark");
        
        repairmanOrderService.acceptOrder(repairmanId, orderId, remark);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/complete")
    public ResponseEntity<?> completeOrder(@RequestBody Map<String, Object> request) {
        Long repairmanId = Long.parseLong(request.get("repairmanId").toString());
        Long orderId = Long.parseLong(request.get("orderId").toString());
        
        repairmanOrderService.completeOrder(repairmanId, orderId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/cancel")
    public ResponseEntity<?> cancelOrder(@RequestBody Map<String, Object> request) {
        Long repairmanId = Long.parseLong(request.get("repairmanId").toString());
        Long orderId = Long.parseLong(request.get("orderId").toString());
        
        repairmanOrderService.cancelOrder(repairmanId, orderId);
        return ResponseEntity.ok().build();
    }
} 